package com.ouheng.shop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.config.Constant;
import com.ouheng.common.domain.Tree;
import com.ouheng.common.utils.R;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductGroupDO;
import com.ouheng.shop.service.ProductGroupService;
import com.ouheng.shop.service.ProductProductGroupService;
import com.ouheng.shop.service.ProductService;

import io.swagger.annotations.ApiOperation;

/**
 * 商品分组
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-20 16:40:56
 */
 
@Controller
@RequestMapping("/shop/productGroup")
public class ProductGroupController {
	@Autowired
	private ProductGroupService productGroupService;
	
	@Autowired
	private ProductProductGroupService productProductGroupService;
	
	@Autowired
	private ProductService productService;
	
	@GetMapping()
	@RequiresPermissions("shop:productGroup:productGroup")
	String ProductGroup(){
	    return "shop/productGroup/productGroup";
	}
	
	@ApiOperation(value="获取商品分组列表", notes="")
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:productGroup:productGroup")
	public List<ProductGroupDO> list(){
		Map<String, Object> query = new HashMap<>(16);
		List<ProductGroupDO> productGroupList = productGroupService.list(query);
		return productGroupList;
	}
	
	@GetMapping("/add/{pId}")
	@RequiresPermissions("shop:productGroup:add")
	String add(@PathVariable("pId") Long pId, Model model){
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "商品总分组");
		} else {
			model.addAttribute("pName", productGroupService.get(pId).getName());
		}
	    return "shop/productGroup/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:productGroup:edit")
	String edit(@PathVariable("id") Long proId,Model model){
		
		ProductGroupDO productGroup = productGroupService.get(proId);
		if(Constant.DEPT_ROOT_ID.equals(productGroup.getParentId())) {
			model.addAttribute("parentProductName", "商品总分组");
		}else {
			ProductGroupDO productGroupDO = productGroupService.get(productGroup.getParentId());
			model.addAttribute("parentProductName", productGroupDO.getName());
		}
		model.addAttribute("productGroup", productGroup);
	    return "shop/productGroup/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("添加商品分组")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:productGroup:add")
	public R save( ProductGroupDO productGroup){
		if(productGroupService.save(productGroup)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("修改商品分组")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:productGroup:edit")
	public R update( ProductGroupDO productGroup){
		productGroupService.update(productGroup);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除商品分组")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:productGroup:remove")
	public R remove( Long proId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", proId);
		if(productGroupService.count(map)>0) {
			return R.error(1, "包含下级分组,不允许修改");
		}
		if(productGroupService.checkGroupHasProduct(proId)) {
			if (productGroupService.remove(proId) > 0) {
				return R.ok();
			}
		}else {
			return R.error(1, "分组包含商品,不允许修改");
		}
		if(productGroupService.remove(proId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("批量删除商品分组")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:productGroup:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		productGroupService.batchRemove(ids);
		return R.ok();
	}
	
	@GetMapping("/tree")
	@ResponseBody
	public Tree<ProductGroupDO> tree() {
		Tree<ProductGroupDO> tree = new Tree<ProductGroupDO>();
		tree = productGroupService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return  "shop/productGroup/productGroupTree";
	}
	
//	@GetMapping("/relevance/{id}")
//	@RequiresPermissions("shop:productGroup:prorelevance")
//	String relevance(@PathVariable("id") Long id,Model model) {
//		ProductGroupDO productGroup = productGroupService.get(id);
//		model.addAttribute("productGroup", productGroup);
//		return "shop/productGroup/relevance";
//	}
	
	@GetMapping("/protree/{productid}")
	@ResponseBody
	@RequiresPermissions("shop:productGroup:prorelevance")
	Tree<ProductDO> protree(@PathVariable("productid") Long productid) {
		Tree<ProductDO> tree = productGroupService.getTree(productid);
		return tree;
	}
	
//	@Log("保存关联商品")
//	@ResponseBody
//	@PostMapping("/saveRelevance")
//	public R saveRelevance(ProductGroupDO productGroup){
//		productGroupService.saveRelevance(productGroup);
//		return R.ok();
//	}
	
	@GetMapping("/relevance/{id}")
	String relevance(@PathVariable("id")Long id,Model model){
		List<Long> ppids = productProductGroupService.listProductidByGroupid(id);
		model.addAttribute("ppids", ppids);
		model.addAttribute("pid", id);
	    return "shop/productGroup/relevance";
	}
	
	@ResponseBody
	@GetMapping("/listrelevance")
	public List<ProductDO> listrelevance(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<ProductDO> productList = productService.list(params);
		return productList;
	}
	
	@Log("保存关联商品")
	@ResponseBody
	@PostMapping("/saveRelevance")
	public R saveRelevance( Long pid,@RequestParam("ppids[]")Long[] ppids){
		productGroupService.saveRelevance(pid,ppids);
		return R.ok();
	}
	
}
